package com.hongyi.excutorservice;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @Author: hongyi
 * @CreateTime: 2024-10-23
 */


public class MyExcutorService {
    public static void main(String[] args) {

        // 创建线程池
        ExecutorService threadPool = Executors.newFixedThreadPool(5);

        String[] names = {"张三", "李四", "王五", "赵六"};

        // 创建提交结果
        Future<String>[] futures = new Future[names.length];

        // 提交任务

        for (int i = 0; i < names.length; i++) {
            String name = names[i];
            futures[i] = threadPool.submit(new MyApiTask(name));
           /* threadPool.execute(() -> {

            });*/
        }


        // 获取结果
        for (int i = 0; i < futures.length; i++) {
            try {
                // 通过 get() 方法获取结果
                System.out.println("返回值：" + futures[i].get());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        // 关闭线程池
        threadPool.shutdown();
    }

}
